package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController("AddressBook 相关接口")
@RequestMapping("/user/addressBook")
@Api(tags = "AddressBook 相关接口")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressService addressService;
    @Autowired
    private ServerProperties serverProperties;

    /**
     * 新增地址
     *
     * @return
     */
    @ApiOperation("新增地址")
    @PostMapping
    public Result<Integer> getStatus(@RequestBody AddressBook addressBook){
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        log.info("新增地址信息: {}", addressBook);
        addressService.insertAddress(addressBook);
        return Result.success();
    }
    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> getUserAllAddress(){
        long userId = BaseContext.getCurrentId();
        List<AddressBook> list = addressService.listByUserId(userId);
        return Result.success(list);
    }
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        long userId = BaseContext.getCurrentId();
        AddressBook addressBook = addressService.getDefault(userId);
        return Result.success(addressBook);
    }
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){ // 前端传递的是json数据为一个对象，需要用包含id属性对象接
//        log.info(o.toString());
        log.info("地址id:{}", addressBook.getId());
        addressService.setDefault(addressBook.getId());
        return new Result<>().success();
    }
    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result delete(Long id){
        addressService.delete(id);
        return Result.success();
    }
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook = addressService.getById(id);
//        log.info("根据id查询获取到的地址信息：{}",addressBook);
        return Result.success(addressBook);
    }
    @ApiOperation("根据id修改地址")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressService.update(addressBook);
        return Result.success();
    }


}

